package com.database;

import java.sql.*;

public class GradesDB extends Database
{
	int studentID;
        int courseNumber;
	int indGrade;
	int groupGrade;
        String comments = null; 
	
	
	public AdministratorDB()
	{
		tableName = "Grades";
	}
	
	public AdministratorDB(int studentID, int courseNumber, int indGrade, int groupGrade, String comments)
	{
		tableName = "Grades";
		
		this.studentID = studentID;
           	this.courseNumber = courseNumber; 
		this.indGrade = indGrade;
		this.groupGrade = groupGrade;
		this.comments = comments;
	}
	
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException, SQLException;
	public boolean UserExists(int id) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where studentID = " + id;
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in databse
		{
			this.studentID = Integer.parseInt(rs.getString("studentID"));
			this.courseNumber = Integer.parseInt(rs.getString("courseNumber"));
    			this.indGrade = Integer.parseInt(rs.getString("indGrade"));
			this.groupGrade = Integer.parseInt(rs.getString("groupGrade"));
			this.comments = rs.getString("comments");
			return true;
		}
		else
		{
			return false;
		}
	}
        //@requires courseNumber !== Null;
    //@assignable newCourseNumber,studentID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newCourseNumber, int tupleID) throws SQLException
	{
		super.Modify(newCourseNumber, "courseNumber","studentID",tupleID); 
	}
	//@requires indGrade !== Null;
    //@assignable newIndGrade,studentID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newIndGrade, int tupleID) throws SQLException
	{
		super.Modify(newIndGrade, "indGrade","studentID",tupleID); 
	}
        //@requires groupGrade !== Null;
    //@assignable newGroupGrade,studentID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newGroupGrade, int tupleID) throws SQLException
	{
		super.Modify(newGroupGrade, "groupGrade","studentID",tupleID); 
	}
        //@requires comments !== Null;
    //@assignable newComments,studentID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newComments, int tupleID) throws SQLException
	{
		super.Modify(newComments, "comments","studentID",tupleID); 
	}
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	@Override
	public void Insert() throws SQLException 
	{
		Statement stmt = connection.createStatement();
				
	    String sqlString = "INSERT INTO " + tableName + " VALUES (" + 
	    					studentID + ", " +
						courseNumber + ", " +
						indGrade + "," +
						groupGrade + "," +
	    					AddQuotes(comments) + ", " +
	    					")";
	    
	    stmt.executeUpdate(sqlString);
	        
	    System.out.println(sqlString + " complete");
	}
	
	@Override
    //@assignable stmt,id,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Delete(String primaryKey) throws SQLException 
	{
		Statement stmt = connection.createStatement();
		int id = Integer.parseInt(primaryKey);
		String sqlString = "DELETE from " + tableName + " WHERE studentID = " + id;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");
	}
	
	@Override
    //@assignable sqlString, stmt, ResultSet;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Display() throws SQLException 
	{
	    String sqlString = "SELECT * FROM " + tableName;
	    Statement stmt = connection.createStatement();

	    ResultSet rs = stmt.executeQuery(sqlString);

	    while (rs.next())
	    {
	        System.out.println
	        (
	            rs.getString("studentID") + " " +
		    rs.getString("courseNumber") + " " +
	            rs.getString("indGrade") + " " +
	            rs.getString("groupGrade") + " " +
	            rs.getString("comments") + " "   
	        );
	    }	
	}

}
